package com.xl.web.controller.dict;

import java.util.List;
import java.util.Map;

import jodd.servlet.URLDecoder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.base.web.BaseController;
import com.xl.core.util.HtmlUtil;
import com.xl.core.util.StringUtil;
import com.xl.web.controller.fns.FnsBaseUtil;
import com.xl.web.entity.dict.DictBase;
import com.xl.web.service.dict.DictService;

/**
 * 系统字典数据管理控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("/sys/dict")
public class DictController extends BaseController {

	// 注入系统字典管理service
	@Autowired(required = false)
	private DictService<DictBase> dictService;

	/**
	 * 字典列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.list")
	@RequestMapping("/list")
	public ModelAndView list(DictBase dictBase) throws Exception {
		Map<String, Object> context = getRootMap(request);
		return forword("dict/dict_list", context);
	}
	
	/**
	 * 字典列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {
		DictBase dict = (DictBase)getJsonParams(DictBase.class);
		// 查询列表
		List<DictBase> dataList = dictService.queryTypeByList(dict);
		sendListData(response, dict.getPager().getRowCount(), dataList);
	}

	/**
	 * 字典值列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.list")
	@RequestMapping("/dictValueList")
	public ModelAndView dictValueList(DictBase dictBase) throws Exception {

		Map<String, Object> context = getRootMap(request);
		context.put("entity", dictBase);
		return forword("dict/dict_value_list", context);
	}
	
	/**
	 * 字典值列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.list")
	@ResponseBody
	@RequestMapping("/listValueData")
	public void listValueData() throws Exception {
		DictBase dict = (DictBase)getJsonParams(DictBase.class);
		// 查询列表
		List<DictBase> dataList = dictService.queryByList(dict);
		sendListData(response, dict.getPager().getRowCount(), dataList);
	}

	/**
	 * 新增字典类型页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@Duplicate(add = true)
	@RequestMapping("/addType")
	public ModelAndView addType() throws Exception {

		Map<String, Object> context = getRootMap(request);

		return forword("dict/dict_add", context);
	}

	/**
	 * 修改字典类型页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@Duplicate(add = true)
	@RequestMapping("/updateType")
	public ModelAndView updateType(DictBase dictBase) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 获取类型的详细数据
		DictBase dictType = dictService.queryByType(dictBase);

		// 字典的数据
		dictBase.setDictTypeName(dictType.getDictTypeName());
		context.put("dictBase", dictBase);

		return forword("dict/dict_update", context);
	}

	/**
	 * 新增字典类型保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addTypeSave")
	public void addTypeSave(DictBase dictBase, String oldDictType)
			throws Exception {

		// 保存
		dictService.addTypeSave(dictBase, oldDictType);

		sendSuccessMessage(response, "保存成功");
	}

	/**
	 * 删除字典类型保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@ResponseBody
	@RequestMapping("deleteDictType")
	public void deleteDictType(DictBase dictBase) throws Exception {

		// 逻辑删除本条数据
		dictService.deleteDictType(dictBase);

		sendSuccessMessage(response, "删除成功");
	}

	/**
	 * 新增或者修改字典值页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@Duplicate(add = true)
	@RequestMapping("/addOrUpdateDict")
	public ModelAndView addOrUpdateDict(DictBase entity) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 字典的数据
		DictBase dictBase = dictService.queryById(entity);
		context.put("dictBase", dictBase);

		// 获取类型的详细数据
		DictBase dictType = dictService.queryByType(entity);
		entity.setDictTypeName(dictType.getDictTypeName());
		context.put("entity", entity);

		return forword("dict/dict_value_update", context);
	}

	/**
	 * 新增字典值保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addDictSave")
	public void addDictSave(DictBase dictBase) throws Exception {

		// 保存
		if (StringUtil.isEmpty(dictBase.getId())) {
			dictService.add(dictBase);
		} else {
			dictService.updateBySelective(dictBase);
		}

		sendSuccessMessage(response, "保存成功");
	}

	/**
	 * 删除字典类型保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.handle")
	@ResponseBody
	@RequestMapping("deleteDict")
	public void deleteDict(DictBase dictBase) throws Exception {

		dictBase = dictService.queryById(dictBase);
		// 删除本条数据
		dictService.delete(dictBase);

		DictBase check = new DictBase();
		check.setDictType(dictBase.getDictType());
		int size = dictService.queryByCount(check);
		if (size > 0) {
			sendSuccessMessage(response, "");
		} else {
			sendSuccessMessage(response, "close");
		}
	}

	/**
	 * 验证字典类型是否已经存在
	 * 
	 * @param request
	 * @param responce
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.dict.list")
	@ResponseBody
	@RequestMapping("/checkDict")
	public void checkMemName(DictBase dictBase, String oldDictType,
			String oldDictTypeName, String oldDictValue, String oldDictName)
			throws Exception {

		// 参数转换，防止中文乱码
		if (!StringUtil.isEmpty(dictBase.getDictValue())) {
			dictBase.setDictValue(URLDecoder.decode(URLDecoder.decode(dictBase
					.getDictValue())));
		}
		if (!StringUtil.isEmpty(dictBase.getDictName())) {
			dictBase.setDictName(URLDecoder.decode(URLDecoder.decode(dictBase
					.getDictName())));
		}
		if (!StringUtil.isEmpty(dictBase.getDictType())) {
			dictBase.setDictType(URLDecoder.decode(URLDecoder.decode(dictBase
					.getDictType())));
		}
		if (!StringUtil.isEmpty(dictBase.getDictTypeName())) {
			dictBase.setDictTypeName(URLDecoder.decode(URLDecoder
					.decode(dictBase.getDictTypeName())));
		}
		// 判断是否与原来的值相同
		if (!StringUtil.isEmpty(oldDictValue)
				&& oldDictValue.equals(dictBase.getDictValue())) {
			HtmlUtil.writerJson(response, true);
			return;
		}
		if (!StringUtil.isEmpty(oldDictName)
				&& oldDictName.equals(dictBase.getDictName())) {
			HtmlUtil.writerJson(response, true);
			return;
		}
		if (!StringUtil.isEmpty(oldDictType)
				&& oldDictType.equals(dictBase.getDictType())) {
			HtmlUtil.writerJson(response, true);
			return;
		}
		if (!StringUtil.isEmpty(oldDictTypeName)
				&& oldDictTypeName.equals(dictBase.getDictTypeName())) {
			HtmlUtil.writerJson(response, true);
			return;
		}

		// 获取到存在的数据
		List<DictBase> checkList = dictService.checkDictExist(dictBase);
		// 如果查询不到，返回true
		if (checkList == null || checkList.size() == 0) {
			HtmlUtil.writerJson(response, true);
			return;
		}

		HtmlUtil.writerJson(response, false);
	}
	
	/**
	 * 获取到字典类型对应的list数据
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getDictList")
	public void getDictList(String type) throws Exception {
        HtmlUtil.writerJson(response, FnsBaseUtil.getDictList(type));
	}
}
